package com.iflytek.exam.controller;


import com.iflytek.common.config.RuoYiConfig;
import com.iflytek.common.core.controller.BaseController;
import com.iflytek.common.core.domain.AjaxResult;
import com.iflytek.common.utils.file.FileUploadUtils;
import com.iflytek.common.utils.file.FileUtils;
import com.iflytek.exam.service.BulkImportService;
import com.iflytek.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/exam/bulk")
public class BulkImportController extends BaseController {

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private BulkImportService bulkImportService;


    //@PreAuthorize("@ss.hasPermi('exam:bulk:upload')")
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    //@PreAuthorize("@ss.hasPermi('exam:bulk:import')")
    @PostMapping("/readUploadedFile")
    public AjaxResult readUploadedFile(Long quBankId, Integer quTypeId, String url) throws Exception {

        //TODO 上线的时候需要修改这个分割的路径
        Pattern pattern = Pattern.compile("upload/(.*)");
        Matcher matcher = pattern.matcher(url);
        String filePath = "";
        if (matcher.find()) {
            filePath = RuoYiConfig.getUploadPath() + "/" + matcher.group(1);
        }
        // 进行文件读取操作
        System.out.println(url);
        try {
            if (quTypeId == 1) {
                bulkImportService.readExcelForChoice(filePath, quBankId, quTypeId,0);
            } else if (quTypeId == 2) {
                bulkImportService.readExcelForJudge(filePath, quBankId, quTypeId,0);
            } else if (quTypeId == 3) {
                bulkImportService.readExcelForShortAnswer(filePath, quBankId, quTypeId,0);
            } else if (quTypeId == 4) {
                bulkImportService.readExcelForChoice(filePath, quBankId, quTypeId,0);
                bulkImportService.readExcelForJudge(filePath, quBankId, quTypeId,1);
                bulkImportService.readExcelForShortAnswer(filePath, quBankId, quTypeId,2);
            }
        } catch (Exception e) {
            return AjaxResult.error("处理过程中发生错误：" + e.getMessage());
        }
        return AjaxResult.success();
    }

}
